package com.campsys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.common.Result;
import com.campsys.entity.User;
import com.campsys.service.TeacherService;
import com.campsys.service.dto.TeacherDTO;
import com.campsys.service.dto.TeacherQueryDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/teacher")
@Slf4j
public class AdminTeacherController {
    
    @Autowired
    private TeacherService teacherService;
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("teacher:view")
    @PostMapping("/list")
    public Result getTeacherList(@RequestBody TeacherQueryDTO queryDTO) {
        Page<User> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        return Result.ok(teacherService.getTeacherList(page, queryDTO));
    }
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("teacher:add")
    @PostMapping("/add")
    public Result addTeacher(@RequestBody @Validated TeacherDTO teacherDTO) {
        teacherService.addTeacher(teacherDTO);
        return Result.ok();
    }
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("teacher:edit")
    @PostMapping("/update")
    public Result updateTeacher(@RequestBody @Validated TeacherDTO teacherDTO) {
        if (teacherDTO.getId() == null) {
            return Result.error("教师ID不能为空");
        }
        teacherService.updateTeacher(teacherDTO);
        return Result.ok();
    }
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("teacher:delete")
    @PostMapping("/delete/{id}")
    public Result deleteTeacher(@PathVariable Long id) {
        teacherService.deleteTeacher(id);
        return Result.ok();
    }
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("teacher:edit")
    @PostMapping("/status/{id}")
    public Result updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        teacherService.updateStatus(id, status);
        return Result.ok();
    }
} 